﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runda.CMS.Dal;
using System.Data;
using System.Xml;
using System.Web;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Log;
using log4net;

namespace Runda.CMS.Bll
{
    public class WFProcessBll:CacheBase
    {
        public string GetProcessEntityXML(string ModuleGUID) {
            WflModelEntity wflModelEntity = new WflModelDal().Get<WflModelEntity>(new Guid(ModuleGUID));

            if (wflModelEntity != null)
            {
                List<WflActivityEntity> wflActivitys = new WflActivityDal().GetAll<WflActivityEntity>(" and ModelID='" + wflModelEntity.ModelID.ToString() + "' order by ActivityType asc");
                foreach (WflActivityEntity wflActivity in wflActivitys)
                {
                    List<WflActivityCheckersEntity> wflActivityCheckers = new WflActivityCheckersDal().GetAll<WflActivityCheckersEntity>(" and ActivityID = '" + wflActivity.ActivityID.ToString() + "'");
                    wflActivity.wflActivityCheckersEntitys = wflActivityCheckers;
                }
                XmlDocument document = new XmlDocument();
                document.Load(System.Web.HttpContext.Current.Server.MapPath(".") + @"\xml\wfprocess.xml");
                document.SelectSingleNode("/Process/Attributes/ProcessGUID").InnerText = wflModelEntity.ModelID.ToString();
                document.SelectSingleNode("/Process/Attributes/Name").InnerText = wflModelEntity.ModelName;
                document.SelectSingleNode("/Process/Attributes/Description").InnerText = wflModelEntity.ModelDescription;
                document.SelectSingleNode("/Process/Attributes/IsActive").InnerText = "1";
                document.SelectSingleNode("/Process/Attributes/BusinessModuleURL").InnerText = "";
                document.SelectSingleNode("/Process/Attributes/BusinessReportURL").InnerText = "";
                document.SelectSingleNode("/Process/Attributes/ProcessBusinessType").InnerText = "";
                document.SelectSingleNode("/Process/Init-Step/Attributes/InitMember").InnerText = "";
                document.SelectSingleNode("/Process/Init-Step/Attributes/InitMemberName").InnerText = "";
                document.SelectSingleNode("/Process/End-Step/Attributes/BookMember").InnerText = "";
                document.SelectSingleNode("/Process/End-Step/Attributes/BookMemberName").InnerText = "";

                XmlNode node3 = document.SelectSingleNode("/Process/Steps");
                XmlNode node = document.SelectSingleNode("/Process/Steps/Step[1]/Nodes/Node");
                foreach (WflActivityEntity wflActivity in wflActivitys)
                {
                    XmlNode node4 = null;
                    bool flag = false;
                    int num3 = node3.ChildNodes.Count - 1;
                    for (int j = 0; j <= num3; j++)
                    {
                        if (node3.ChildNodes.Item(j).SelectSingleNode("Attributes/StepID").InnerText ==wflActivity.ActivityID.ToString())
                        {
                            node4 = node3.ChildNodes.Item(j);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        node4 = node3.FirstChild.Clone();
                        node4.SelectSingleNode("Nodes").RemoveChild(node4.SelectSingleNode("Nodes").FirstChild);
                        node4.SelectSingleNode("Attributes/StepID").InnerText = wflActivity.ActivityID.ToString();
                        node4.SelectSingleNode("Attributes/Name").InnerText = wflActivity.ActivityName;
                        node4.SelectSingleNode("Attributes/StepType").InnerText = "0";
                        node4.SelectSingleNode("Attributes/IsStay").InnerText = "0";
                        node4.SelectSingleNode("Attributes/ThresholdValue").InnerText = "";
                        node3.AppendChild(node4);
                    }
                    XmlNode newChild = node.Clone();
                    string auditors = "";
                    foreach(WflActivityCheckersEntity wflActivityChecker in wflActivity.wflActivityCheckersEntitys){
                        if (auditors != "") {
                            auditors += ";";
                        }
                        auditors += wflActivityChecker.UserName;
                    }
                    newChild.SelectSingleNode("Attributes/Auditor").InnerText = auditors;
                    newChild.SelectSingleNode("Attributes/AuditorName").InnerText = auditors;
                    newChild.SelectSingleNode("Attributes/RightValue").InnerText = "1";
                    newChild.SelectSingleNode("Attributes/AlertDay").InnerText = "2";
                    newChild.SelectSingleNode("Attributes/NotifyDay").InnerText = "2";
                    node4.SelectSingleNode("Nodes").AppendChild(newChild);
                }
                node3.RemoveChild(node3.FirstChild);
                return document.OuterXml;
            }
            return "";
        }

        public List<SysUserEntity> getUsersByActivity(Guid ActivityID)
        {
            List<SysUserEntity> users = new List<SysUserEntity>();
            users = new SysUserBll().GetAll<SysUserEntity>(" and UserID in ( select UserID from Cms_Wfl_ActivityCheckers where ActivityID = '" + ActivityID.ToString() + "')");
            return users;
        }
    }
}
